package com.chinasoft.ssm.controller;


import com.chinasoft.ssm.mapper.BikeMapper;
import com.chinasoft.ssm.pojo.Bike;
import com.chinasoft.ssm.pojo.Result;
import com.chinasoft.ssm.service.BikeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@Slf4j
public class BikeController {
    @Autowired
    BikeService bikeService;
    @Autowired
    BikeMapper mapper;

    @GetMapping("/bike")
    public Result all()
    {
        Result result = new Result();
        List<Bike> bikes = bikeService.selectAll();
        result.setData(bikes);
        result.setMessage("全表查询");
        result.setStatus(200);
        return result;

    }
    @GetMapping("/bike15")
    public Result bikelist()
    {
        Result result = new Result();
        List<Bike> bikes = bikeService.select15();
         result.setStatus(200);
         result.setData(bikes);
         return result;

    }
    @GetMapping("/bikesel/{id}")
    public Result one(@PathVariable Integer id)
    {
        Result result = new Result();
        Bike bike = bikeService.select(id);
        result.setData(bike);
        result.setMessage("查询成功");
        result.setStatus(200);
        return result;
    }
    @GetMapping("/Abikesel/{id}")
    public Result Aone(@PathVariable Integer id)
    {
        Result result = new Result();
        Bike bike = bikeService.select(id);
        result.setData(bike);
        result.setMessage("查询成功");
        result.setStatus(200);
        return result;
    }
    @DeleteMapping("/bikedel/{id}")
    public Result delete(@PathVariable Integer id)
    {
        Result result = new Result();
        Integer data = bikeService.delete(id);
        result.setData(data);
        result.setMessage("删除成功");
        result.setStatus(200);
        return result;
    }
    @DeleteMapping("/Abikedel/{id}")
    public Result Adelete(@PathVariable Integer id)
    {
        Result result = new Result();
        Integer data = bikeService.delete(id);
        result.setData(data);
        result.setMessage("删除成功");
        result.setStatus(200);
        return result;
    }
    @PostMapping("/deleteMore")
    Result deleteMore(@RequestBody int[] ids) {

        Result result = new Result();
        int data=0;
        for(int i:ids){
            data+=bikeService.delete(i);
        }
        result.setData(data);
        result.setMessage("分页成功");
        result.setStatus(200);
        return result;
    }
    @GetMapping("/Abike/selectPage/{current}/{size}")
    Result selectPage(@PathVariable Integer current,@PathVariable Integer size) {
        System.out.println(current + " " + size);
        // 计算从第几条记录开始
        Result result = new Result();
//        Integer start = (current - 1) * size;
        List<Bike> brand = mapper.findAll();
        int total = brand.size();
        result.setTotal(total);
        PageHelper.startPage(current, size);

        List<Bike> brands = mapper.findAll();
        // Page 类 是 List的子类
        Page<Bike> info = (Page<Bike>) brands;
        result.setData(info);
        result.setMessage("分页成功");
        result.setStatus(200);
        return result;
    }

    @PostMapping("/AbikeSelDsql/{current}/{size}")
    Result selectPage(@PathVariable Integer current, @PathVariable Integer size,@RequestBody Bike bike) {
        System.out.println(current + " " + size);
        // 计算从第几条记录开始
//        Integer start = (current - 1) * size;
        Result result = new Result();
        List<Bike> bike1 = mapper.selectByDSql(bike);
        int total = bike1.size();
        result.setTotal(total);
        PageHelper.startPage(current, size);

        List<Bike> bikes = mapper.selectByDSql(bike);


        // Page 类 是 List的子类
        Page<Bike> info = (Page<Bike>) bikes;

        result.setData(info);
        result.setMessage("分页成功");
        result.setStatus(200);
        return result;
    }

    @GetMapping("/bikecat/{category}")
    public Result cat(@PathVariable String category) {
        Result result = new Result();
        List<Bike> data = bikeService.selectcat(category);
        result.setData(data);
        result.setMessage("查询成功");
        result.setStatus(200);
        return result;
    }
    @PostMapping("/bikeins")
    public Result insert(@RequestBody Bike bike) {
        Result result = new Result();
        Integer data = bikeService.insert(bike);
        result.setData(data);
        result.setMessage("插入成功");
        result.setStatus(200);
        return result;
    }
    @PostMapping("/Abikeins")
    public Result Ainsert(@RequestBody Bike bike) {
        Result result = new Result();
        bike.setCount(0);
        Integer data = bikeService.insert(bike);
        result.setData(data);
        result.setMessage("插入成功");
        result.setStatus(200);
        return result;
    }
        @PostMapping("/bikeupd")
        public Result update(@RequestBody Bike bike)
        {
            Result result = new Result();
            Integer data = bikeService.update(bike);
            result.setData(data);
            result.setMessage("修改成功");
            result.setStatus(200);
            return result;
        }

    @PostMapping("/Abikeupd")
    public Result Aupdate(@RequestBody Bike bike)
    {
        Result result = new Result();
        Integer data = bikeService.update(bike);
        result.setData(data);
        result.setMessage("修改成功");
        result.setStatus(200);
        return result;
    }





    @GetMapping("/bike/selectPage/{current}/{size}")
    Result selectPage2(@PathVariable Integer current,@PathVariable Integer size) {
        System.out.println(current + " " + size);
        // 计算从第几条记录开始
//        Integer start = (current - 1) * size;

        PageHelper.startPage(current, size);

        Result result = new Result();
        List<Bike> brands = mapper.findAll();
        int total = brands.size();
        result.setTotal(total);
        // Page 类 是 List的子类
        Page<Bike> info = (Page<Bike>) brands;
        result.setData(info);
        result.setMessage("分页成功");
        result.setStatus(200);
        return result;
    }

    @PostMapping("/bikeSelDsql/{current}/{size}")
    Result selectPage1(@PathVariable Integer current, @PathVariable Integer size, @RequestBody Bike bike) {
        System.out.println(current + " " + size);
        // 计算从第几条记录开始
//        Integer start = (current - 1) * size;
        PageHelper.startPage(current, size);

        Result result = new Result();
        List<Bike> bikes = mapper.selectByDSql1(bike);
        int total = bikes.size();
        result.setTotal(total);
        // Page 类 是 List的子类
        Page<Bike> info = (Page<Bike>) bikes;
        result.setData(info);
        result.setMessage("分页成功");
        result.setStatus(200);
        return result;
    }

}
